package com.sumainfor.pc.gmio.service.impl;

import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.gmio.dao.HoLivecardMapper;
import com.sumainfor.pc.gmio.dao.HoTenantBillMapper;
import com.sumainfor.pc.gmio.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.gmio.service.HoLivecardService;
import com.sumainfor.pc.gmio.service.HoTenantBillService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 服务管家租客管理
 */
@Service
public class HoLivecardSerivceImpl implements HoLivecardService {

    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(HoLivecardSerivceImpl.class);
    @Autowired
    private HoLivecardMapper hoLivecardMapper;
    @Autowired
    private ZpcMktFeign zpcMktFeign;
    /**
     * 查询居住证列表
     */
    public List<Map<String, Object>> getHoLivecardList(Map<String, Object> params) {
        List<Map<String, Object>> gmioList = hoLivecardMapper.getHoLivecardList(params);
        //b保存录入账单的服务秘书的编号
        List<String> LiIdList = new ArrayList<String>();
        for (Map<String, Object> map : gmioList) {
            if (map.get("HouInfoId") != null) {
                LiIdList.add(map.get("HouInfoId").toString());
            }
        }
        params.put("LiIdList",LiIdList);
        JsonResult result=zpcMktFeign.getHoulivecardList(params);
        List<Map<String,Object>> marketList=(List<Map<String,Object>>)result.get("result");
        for (Map<String,Object> marketMap: marketList){
            for (Map<String,Object> gmioMap : gmioList) {
                if (MapUtils.getString(marketMap, "HouInfoId").equals(MapUtils.getString(gmioMap, "HouInfoId"))) {
                    gmioMap.putAll(marketMap);
                }
            }
        }
        return gmioList;
    }

    /**
     * 查询居住证的列表总数
     */
    public Integer getHoLivecardListCount(Map<String, Object> params) {
        return hoLivecardMapper.getHoLivecardListCount(params);
    }

    /**
     * 办理居住证
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult updateHoLivecard(Map<String, Object> params) {
        JsonResult result=new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try{
            try {
                params.put("Status",2);//申请状态  1.待处理  2.已处理
                params.put("by", ContextUtils.getUserId());//操作人
                params.put("term", "2");// 更新端 Update端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                params.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonUpdateParms(params);
                params.put("ShelfId",params.get("insby"));//房源上架人编号
                if (hoLivecardMapper.updateHoLivecard(params)>0){
                    result.putSuccess("操作成功");
                }else{
                    result.putFailed("服务器繁忙");
                }
            } catch (Exception e) {
                result.putFailed("服务器繁忙");
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        finally {
            lock.unlock();
        }
        return result;
    }
}
